package com.Fh_learner.Array;

import java.util.Arrays;
import java.util.Comparator;

//练习：自己定制一个
public class ArraySortCustom {
    public static void main(String[] args) {
        int[] arr = {1,-2,6,3,-5,2};
//        bubble01(arr);
//        System.out.println("====传统方法排序后====");
//        System.out.println(Arrays.toString(arr));
        bubble02(arr,new Comparator(){
            public int compare(Object o1, Object o2) {
            Integer i1 = (Integer) o1;
            Integer i2 = (Integer) o2;
            return i2-i1;
            }
        });
        System.out.println("====定制方法排序后====");
        System.out.println(Arrays.toString(arr));
    }
    //传统方法
    public static void bubble01(int[] arr){
        int temp = 0;
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-i-1; j++) {
                if(arr[j]>arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
    //定制方法
    public static void bubble02(int[] arr,Comparator c){
        int temp = 0;
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-i-1; j++) {
                if(c.compare(arr[j],arr[j+1])>0){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }

    }
}
